package com.zhkjnet.zhyy.service;

import com.zhkjnet.zhyy.dao.AssetDao;
import com.zhkjnet.zhyy.dao.MoneyRecordDao;
import com.zhkjnet.zhyy.dao.RechargeRecordDao;
import com.zhkjnet.zhyy.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AssetService {

    @Autowired
    private AssetDao assetDao;

    @Autowired
    private RechargeRecordDao rechargeRecordDao;

    @Autowired
    private MoneyRecordDao moneyRecordDao;

    @Transactional
    public void rechargeById(Long recordId, User transactor) {
        RechargeRecord record = rechargeRecordDao.findByIdWithUser(recordId);
        record.setStatus(RechargeStatus.Success);
        record.setTransactTime(new Date());
        record.setTransactor(transactor);
        rechargeRecordDao.update(record);
        Asset asset = assetDao.findByUserId(record.getUser().getId());
        asset.setUser(record.getUser());
        //加上充值金额
        asset.setMoney(asset.getMoney() + record.getMoney());
        //每天返现金额
        //asset.setCashback(asset.getMoney() / 30);
        //releaseCycle
        if(asset.getReleaseCycle() != null && asset.getReleaseCycle() != 0) {
        	asset.setCashback(asset.getMoney() / asset.getReleaseCycle());
        }else {
        	asset.setCashback(asset.getMoney() / 30);
        }
        
        //最低返现金额
        asset.setMinCashback(asset.getMoney() / 2);
        assetDao.update(asset);
        //记录总金额
        MoneyRecord moneyRecord = new MoneyRecord();
        moneyRecord.setUser(record.getUser());
        moneyRecord.setMoney(record.getMoney());
        moneyRecord.setMessage("充值");
        moneyRecord.setType(MoneyRecordType.All);
        moneyRecordDao.save(moneyRecord);

        _cashback(asset);
    }

    /**
     * 油卡充值失败恢复
     * @param recharge
     */
    @Transactional
    public void oilRechargeFailReset(OilRecharge recharge) {
        Asset asset = assetDao.findAssetByOilRechargeId(recharge.getId());
        asset.setAbleMoney(asset.getAbleMoney() + recharge.getMoney());
        assetDao.update(asset);
        MoneyRecord moneyRecord = new MoneyRecord();
        moneyRecord.setMoney(recharge.getMoney());
        moneyRecord.setType(MoneyRecordType.Able);
        moneyRecord.setMessage("油卡充值失败退款");
        moneyRecordDao.save(moneyRecord);
    }

    @Transactional
    public void updateMinMoneyByUserId(Long userId, Long minMoney) {
        Asset asset = new Asset();
        asset.setMinMoney(minMoney);
        User user = new User();
        user.setId(userId);
        asset.setUser(user);
        assetDao.updateByUserId(asset);
    }

    @Transactional
    public Asset findByUserId(Long userId) {
        return assetDao.findByUserId(userId);
    }

    /**
     * 获取最小充值金额
     * @param userId
     * @return
     */
    public Long findMinRechargeMoneyByUserId(Long userId) {
        Asset asset = assetDao.findByUserId(userId);
        Long money = asset.getMinMoney() - asset.getMoney();
        return money > 0 ? money : 0;
    }

    @Transactional
    public void update(Asset asset) {
        assetDao.update(asset);
    }

    public List<Asset> findAllCashbackAsset() {
        return assetDao.findAllCashbackAsset();
    }

    private void _cashback(Asset asset) {
        Long cashback = asset.getMoney() >= asset.getCashback() ? asset.getCashback() : asset.getMoney();
        MoneyRecord ableRecord = new MoneyRecord();
        ableRecord.setUser(asset.getUser());
        ableRecord.setMoney(cashback);
        ableRecord.setMessage("可用资金到账");
        ableRecord.setType(MoneyRecordType.Able);
        moneyRecordDao.save(ableRecord);

        MoneyRecord allRecord = new MoneyRecord();
        allRecord.setUser(asset.getUser());
        allRecord.setMoney(- cashback);
        allRecord.setMessage("总资金减少");
        allRecord.setType(MoneyRecordType.All);
        moneyRecordDao.save(allRecord);

        assetDao.sumFixedMoneyById(asset.getId(), cashback);
    }

    /**
     * 固定返现
     * @param asset
     */
    @Transactional
    public void cashback(Asset asset) {
       _cashback(asset);
    }

    /**
     * 单笔返现
     */
    @Transactional
    public void single(RechargeRecord rechargeRecord) {
        MoneyRecord moneyRecord = new MoneyRecord();
        moneyRecord.setUser(rechargeRecord.getUser());
        moneyRecord.setMoney(rechargeRecord.getMoney() / 10);
        moneyRecord.setType(MoneyRecordType.Able);
        moneyRecord.setMessage("充值返现10%");
        moneyRecordDao.save(moneyRecord);
        assetDao.sumSignMoneyByUserId(rechargeRecord.getUser().getId(), rechargeRecord.getMoney() / 10);
    }

    public Asset findAssetByOilRechargeId(Long rechargeId) {
        return assetDao.findAssetByOilRechargeId(rechargeId);
    }

    /**
     * 应该提醒
     * @return
     */
    public List<Asset> findAllRemind() {
        return assetDao.findAllRemind();
    }

}